package com.rays.gank.ui.fragment

import android.os.Bundle
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayoutMediator
import com.rays.gank.R
import com.rays.gank.databinding.HomeFragmentBinding
import com.rays.gank.delegate.viewBinding
import com.rays.gank.ui.fragment.home.WanAndroidFragment

/**
 * 首页
 * Created by Rays on 2021/1/23.
 */
class HomeFragment : BaseVbFragment() {
    private val homeFragmentBinding: HomeFragmentBinding by viewBinding()

    override fun getLayoutResId() = R.layout.home_fragment

    override fun initView(savedInstanceState: Bundle?) {
        homeFragmentBinding.appbar.toolbar.title = "首页"

        val fragments = listOf<Fragment>(
            WanAndroidFragment(),
            WanAndroidFragment(),
            WanAndroidFragment(),
            WanAndroidFragment()
        )
        with(homeFragmentBinding.viewPager) {
            isUserInputEnabled = true
            adapter = object : FragmentStateAdapter(this@HomeFragment) {
                override fun getItemCount() = fragments.size

                override fun createFragment(position: Int) = fragments[position]
            }
        }

        val tabTitles = resources.getStringArray(R.array.wan_android_tabs)
        TabLayoutMediator(
            homeFragmentBinding.tabLayout,
            homeFragmentBinding.viewPager
        ) { tab, position ->
            tab.text = tabTitles[position]
        }.attach()
    }
}